#include "comm_type.h"
#include "ui_fb.h"
#include "bitmap.h"
#include <unistd.h>


extern void fb_ui_ShowBitmap(int x,int y,bitmap_t *pBitmap);


int main(int argc, char* argv[]) {
	bitmap_t* pMap;
	printf("-----------open_screen-/dev/fb0-------------------\n");
	if(0>open_screen("/dev/fb0"))
		return 1;
	printf("-----------bitmap_load-map/001.png-------------------\n");
	pMap = bitmap_load("map/001.png");
	if(pMap)
	{
		QRcode_Msg tQrcode;
		//fb_ui_ShowBitmap(400,0,pMap);
		fb_ui_set_rect(450,0,pMap->w,pMap->h,pMap->data);
		sleep(1);
		if(AnalysisQRcode(pMap,&tQrcode) == 3)
		{
			float len1,len3,i;//lenx
			float Sx,Ex,Sy,Ey;
			//------------------确定0号点-------------------------------------
			do
			{
				u16 len01x,len01y,len02x,len02y,len12x,len12y,xybuf;
				u8 x01,y01,x12,y12;
				if(tQrcode.Posit[0].x > tQrcode.Posit[1].x)
					len01x = tQrcode.Posit[0].x - tQrcode.Posit[1].x;
				else
					len01x = tQrcode.Posit[1].x - tQrcode.Posit[0].x;
				
				if(tQrcode.Posit[0].y > tQrcode.Posit[2].y)
					len02y = tQrcode.Posit[0].y - tQrcode.Posit[2].y;
				else
					len02y = tQrcode.Posit[2].y - tQrcode.Posit[0].y;

				x01 = 0;
				if(len01x == len02y){
					x01 = 1;
				}
				else if(len01x > len02y){
					if(((len01x- len02y) << LEN_TOLERANCE) < len01x)
						x01 = 2;
				}
				else{
					if(((len02y - len01x) << LEN_TOLERANCE) < len02y)
						x01 = 3;
				}
				if(x01)
				{
					if(tQrcode.Posit[0].y > tQrcode.Posit[1].y)
						len01y = tQrcode.Posit[0].y - tQrcode.Posit[1].y;
					else
						len01y = tQrcode.Posit[1].y - tQrcode.Posit[0].y;
					
					if(tQrcode.Posit[0].x > tQrcode.Posit[2].x)
						len02x = tQrcode.Posit[0].x - tQrcode.Posit[2].x;
					else
						len02x = tQrcode.Posit[2].x - tQrcode.Posit[0].x;
					y01=0;
					if(len01y == len02x)
						y01 = 1;
					else if(len01y > len02x) {
						if(((len01y- len02x) << LEN_TOLERANCE) < len01y)
							y01 = 2;
					}
					else {
						if(((len02x - len01y) << LEN_TOLERANCE) < len02x)
							y01 = 3;
					}
					if(y01) break; // 0号点
				}
				else
				{
					if(tQrcode.Posit[1].y > tQrcode.Posit[2].y)
						len12y = tQrcode.Posit[1].y - tQrcode.Posit[2].y;
					else
						len12y = tQrcode.Posit[2].y - tQrcode.Posit[1].y;

					x12 = 0;
					if(len01x == len12y){
						x12 = 1;
					}
					else if(len01x > len12y){
						if(((len01x- len12y) << LEN_TOLERANCE) < len01x)
							x12 = 2;
					}
					else{
						if(((len12y - len01x) << LEN_TOLERANCE) < len12y)
							x12 = 3;
					}
					if(x12)
					{

						if(tQrcode.Posit[0].y > tQrcode.Posit[1].y)
							len01y = tQrcode.Posit[0].y - tQrcode.Posit[1].y;
						else
							len01y = tQrcode.Posit[1].y - tQrcode.Posit[0].y;
						
						if(tQrcode.Posit[1].x > tQrcode.Posit[2].x)
							len12x = tQrcode.Posit[1].x - tQrcode.Posit[2].x;
						else
							len12x = tQrcode.Posit[2].x - tQrcode.Posit[1].x;
						y12 = 0;
						if(len01y == len12x){
							y12 = 1;
						}
						else if(len01y > len12x){
							if(((len01y- len12x) << LEN_TOLERANCE) < len01y)
								y12 = 2;
						}
						else{
							if(((len12x - len01y) << LEN_TOLERANCE) < len12x)
								y12 = 3;
						}
						if(y12) 
						{
							xybuf=tQrcode.Posit[0].x;
							tQrcode.Posit[0].x = tQrcode.Posit[1].x;
							tQrcode.Posit[1].x = xybuf;
							xybuf=tQrcode.Posit[0].y;
							tQrcode.Posit[0].y = tQrcode.Posit[1].y;
							tQrcode.Posit[1].y = xybuf;
							break;// 1号点
						}
					}
					xybuf=tQrcode.Posit[0].x;
					tQrcode.Posit[0].x = tQrcode.Posit[2].x;
					tQrcode.Posit[2].x = xybuf;
					xybuf=tQrcode.Posit[0].y;
					tQrcode.Posit[0].y = tQrcode.Posit[2].y;
					tQrcode.Posit[2].y = xybuf;
					break;// 2号点
				}
			}
			while(0);
			//------------确定向量------------------------
			{
				s16 x01,y01,x02,y02;
				u8 xy1,xy2,inxy;
				x01=tQrcode.Posit[1].x-tQrcode.Posit[0].x;
				y01=tQrcode.Posit[1].y-tQrcode.Posit[0].y;
				x02=tQrcode.Posit[2].x-tQrcode.Posit[0].x;
				y02=tQrcode.Posit[2].y-tQrcode.Posit[0].y;
				if(x01 >= 0)
				{
					if(y01 >=0)
						xy1 = 1;
					else
						xy1 = 4;
				}
				else 
				{
					if(y01 >=0)
						xy1 = 2;
					else
						xy1 = 3;
				}

				if(x02 >= 0)
				{
					if(y02 >=0)
						xy2 = 1;
					else
						xy2 = 4;
				}
				else 
				{
					if(y02 >=0)
						xy2 = 2;
					else
						xy2 = 3;
				}
				if((0x03&(xy1-xy2))==0x01)
					inxy = 1;
				else 
					inxy = 2;
				
			}

			

	
			sleep(1);
			fb_ui_line(450+tQrcode.Posit[0].x,300+tQrcode.Posit[0].y,450+tQrcode.Posit[1].x,300+tQrcode.Posit[1].y,RGB_CURR(255, 0, 0));
			fb_ui_line(450+tQrcode.Posit[0].x,300+tQrcode.Posit[0].y,450+tQrcode.Posit[2].x,300+tQrcode.Posit[2].y,RGB_CURR(255, 0, 0));
			//----------------------------------------------------------------------
			len3 = (tQrcode.lenCD[0]+tQrcode.lenCD[1]+tQrcode.lenCD[2])/3;
			len1 = len3/3;
			Sx = tQrcode.Posit[0].x - len3;
			Ex = tQrcode.Posit[1].x + len3;
			Sy = tQrcode.Posit[0].y - len3;
			Ey = tQrcode.Posit[2].y + len3;
			sleep(1);
			//lenx = Ex - Sx;
			for(i=Sx ; i<Ex ; i+= len1)
			{
				fb_ui_line(450+i,300+Sy,450+i,300+Ey,RGB_CURR(255, 0, 255));
			}

			for(i=Sy ; i<Ey ; i+= len1)
			{
				fb_ui_line(450+Sx,300+i,450+Ex,300+i,RGB_CURR(255, 0, 255));
			}
			free(tQrcode.bit);
		}
		
		bitmap_destroy(pMap);
	}

	close_screen();
	return 0;
}

float Q_rsqrt( float number )
 {
   long i;
   float x2, y;
   const float threehalfs = 1.5F;
   x2 = number * 0.5F;
   y  = number;
   i  = * ( long * ) &y;  // evil floating point bit level hacking
   i  = 0x5f3759df - ( i >> 1 ); // what the fuck?
   y  = * ( float * ) &i;
   y  = y * ( threehalfs - ( x2 * y * y ) ); // 1st iteration
  
   #ifndef Q3_VM
   #ifdef __linux__
     assert( !isnan(y) ); // bk010122 - FPE?
   #endif
   #endif
   return y;
 }


float InvSqrt (float x)
{   
	float y,xhalf = 0.5f*x;    
	int i = *(int*)&x;    
	i = 0x5f3759df - (i>>1);    
	y = *(float*)&i;    
	y = y*(1.5f - xhalf*x*x);    
	return y;
}


